package com.itheima.leetcode.od.b.violentenumeration;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * (B卷,100分)- 构成正方形的数量（Java & JS & Python）
 * <p>
 * 题目描述
 * <p>
 * 输入N个互不相同的二维整数坐标，求这N个坐标可以构成的正方形数量。[内积为零的的两个向量垂直]
 * <p>
 * 输入描述
 * <p>
 * 第一行输入为N，N代表坐标数量，N为正整数。N <= 100
 * <p>
 * 之后的 K 行输入为坐标x y以空格分隔，x，y为整数，-10<=x, y<=10
 * <p>
 * 输出描述
 * <p>
 * 输出可以构成的正方形数量。
 * <p>
 * 用例
 * <p>
 * 输入
 * <p>
 * 3
 * <p>
 * 1 3
 * <p>
 * 2 4
 * <p>
 * 3 1
 * <p>
 * 输出	0 （3个点不足以构成正方形）
 * 说明	无
 * <p>
 * 输入
 * <p>
 * 4
 * <p>
 * 0 0
 * <p>
 * 1 2
 * <p>
 * 3 1
 * <p>
 * 2 -1
 * <p>
 * 输出	1
 * 说明	无
 */
public class NumberOfSquaresFormed {
    public static void main(String[] args) {
        /*Scanner sc = new Scanner(System.in);

        int n = Integer.parseInt(sc.nextLine());

        String[] coordinates = new String[n];
        for (int i = 0; i < n; i++) {
            coordinates[i] = sc.nextLine();
        }*/

        int n = 4;

        int[][] coordinates = Arrays.stream("0 0\n1 2\n3 1\n2 -1".split("\n"))
                .map(s -> Arrays.stream(s.split(" "))
                        .map(Integer::parseInt)
                        .toArray())
                .toArray(int[][]::new);

        System.out.println(getResult(n, coordinates));
    }

    public static int getResult(int n, int[][] coordinates) {
        int squareCount = 0;

        Set<String> set = Arrays.stream(coordinates).map(arr -> arr[0] + " " + arr[1]).collect(Collectors.toSet());

        for (int i = 0; i < n; i++) {
            int[] arr1 = coordinates[i];
            int x1 = arr1[0];
            int y1 = arr1[1];

            for (int j = i + 1; j < n; j++) {
                int[] arr2 = coordinates[j];
                int x2 = arr2[0];
                int y2 = arr2[1];

                int x3 = x1 - (y1 - y2);
                int y3 = y1 + (x1 - x2);
                int x4 = x2 - (y1 - y2);
                int y4 = y2 + (x1 - x2);
                if (set.contains(x3 + " " + y3) && set.contains(x4 + " " + y4)) {
                    squareCount++;
                }

                int x5 = x1 + (y1 - y2);
                int y5 = y1 - (x1 - x2);
                int x6 = x2 + (y1 - y2);
                int y6 = y2 - (x1 - x2);
                if (set.contains(x5 + " " + y5) && set.contains(x6 + " " + y6)) {
                    squareCount++;
                }
            }
        }

        return squareCount / 4;
    }
}